using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SSOTasks = BizTalk.BuildGenerator.Tasks.SSO;
using System.Diagnostics;
using System.IO;
using BizTalk.BuildGenerator.Tasks.SSO;

namespace BizTalk.BuildGenerator.Tasks.Tests.SSO
{
    /// <summary>
    /// Summary description for SSOTests
    /// </summary>
    [TestClass]
    public class SSOTests : MSBuildTest 
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public SSOTests()
        {

        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Pretest setup
        /// </summary>
        [TestInitialize]
        public void Setup()
        {            
            base.GetFileAndWriteToPath("BizTalk.BuildGenerator.Tasks.Tests.SSO.SSOTestBuildScript.msbuild", "SSOTestBuildScript.msbuild");
            base.GetFileAndWriteToPath("BizTalk.BuildGenerator.Tasks.Tests.SSO.SampleSSOTestData.xml", "SampleSSOTestData.xml");
        }
        /// <summary>
        /// Cleans after test
        /// </summary>
        [TestCleanup]
        public void CleanUp()
        {
            File.Delete("SSOTestBuildScript.msbuild");
            File.Delete("SampleSSOTestData.xml");            
        }
        /// <summary>
        /// Tests the SSO tasks
        /// </summary>
        [TestMethod]
        public void ExecuteSSOMsBuildTest()
        {
            const string SSOApplicationName = "BizTalkBuildGeneratorTestApplication";

            string arguments = "SSOTestBuildScript.msbuild /t:Build";
            int result = RunMSBuild(arguments);
            Assert.IsTrue(result == 0, "The MSBuild Task failed.");

            string val = SSOConfiguration.Read(SSOApplicationName, "CustomProperty1");
            Assert.IsNotNull(val, "The property value should not be null");
            Assert.IsTrue(val == "This is a sample text value", "The value is not as expected");

            val = SSOConfiguration.Read(SSOApplicationName, "CustomProperty2");
            Assert.IsNotNull(val, "The property2 value should not be null");
            Assert.IsTrue(val == File.ReadAllText("SampleSSOTestData.xml"), "The value is not as expected");

            DeleteApplication deleteApp = new DeleteApplication();
            deleteApp.ApplicationName = SSOApplicationName;
        }
    }
}
